package View;

import java.awt.Desktop;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.*;

import Controller.MouseAction;
import Model.Contact;

/**
 * Standard view for a contact
 * 
 * @author Vincent Francolin
 * @version 1.0 : creation
 */
public class ContactView extends JPanel {
	private JLabel name;
	private JLabel firstName;
	private JLabel companyAddress;
	private JLabel mobilePhone;
	private JLabel homePhone;
	private JLabel mobilePhonePro;
	private JLabel officePhone;
	private JLabel homePage;
	private JLabel mailPro;
	private JLabel mailPerso;
	private ImageIcon pic;
	private JLabel other;

	/**
	 * default constructor
	 * 
	 * @param c
	 *            contact data in case of modification
	 */
	public ContactView(Contact c) {
		super();
		this.name = new JLabel(c.getName());
		this.firstName = new JLabel(c.getFirstName());
		this.companyAddress = new JLabel(c.getCompanyAddress());
		this.mobilePhone = new JLabel(c.getMobilePhone());
		this.homePhone = new JLabel(c.getHomePhone());
		this.mobilePhonePro = new JLabel(c.getMobilePhonePro());
		this.officePhone = new JLabel(c.getOfficePhone());
		this.homePage = new JLabel(c.getHomePage());
		this.mailPro = new JLabel(c.getMailPro());
		this.mailPerso = new JLabel(c.getMailPerso());
		this.other = new JLabel(c.getOther());
		
		pic = new ImageIcon(new ImageIcon("images/"+c.getPicPath()).getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT));
		if(pic.getIconHeight()==-1)//fail to open image we open default
			pic = new ImageIcon(new ImageIcon("images/notFound.png").getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT));
		initView();
	}

	/**
	 * initialize the view of the panel
	 */
	private void initView() {
		this.setLayout(new GridBagLayout());
		GridBagConstraints cons = new GridBagConstraints();

		cons.gridy = 0;
		cons.gridx = 0;
		add(new JLabel(pic), cons);
		
		cons.gridy++;
		cons.insets = new Insets(5, 5, 5, 10);
		cons.gridx = 0;
		add(new JLabel("Name : "), cons);
		cons.gridx = 1;
		add(name, cons);
		cons.gridx = 2;
		add(new JLabel("First name : "), cons);
		cons.gridx = 3;
		add(firstName, cons);

		cons.insets = new Insets(5, 5, 1, 5);
		cons.gridy++;
		cons.gridx = 0;
		add(new JLabel("Office : "), cons);

		cons.gridy++;
		add(new JLabel("Company address : "), cons);
		cons.gridx = 1;
		add(companyAddress, cons);

		cons.gridy++;
		cons.gridx = 0;
		cons.gridwidth = 1;
		add(new JLabel("Office phone : "), cons);
		cons.gridx = 1;
		add(officePhone, cons);
		cons.gridx = 2;
		add(new JLabel("Professional mobile : "), cons);
		cons.gridx = 3;
		add(mobilePhonePro, cons);

		cons.insets = new Insets(5, 5, 5, 5);
		cons.gridy++;
		cons.gridx = 0;
		add(new JLabel("Office mail : "), cons);
		cons.gridx = 1;
		add(mailPro, cons);
		mailPro.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				if (Desktop.isDesktopSupported()) {
					try {
						Desktop.getDesktop().mail(
								new URI("mailto:" + mailPro.getText()));
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						JOptionPane
								.showMessageDialog(null,
										"Error : no mail client specified or an error occured");
					} catch (URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						JOptionPane
								.showMessageDialog(null,
										"Error : no mail client specified or an error occured");
					}
				}
			}

		});

		cons.gridy++;
		cons.gridx = 0;
		add(new JLabel("Personnal : "), cons);

		cons.insets = new Insets(5, 5, 1, 5);
		cons.gridy++;
		cons.gridx = 0;
		add(new JLabel("Home phone : "), cons);
		cons.gridx = 1;
		add(homePhone, cons);
		cons.gridx = 2;
		add(new JLabel("Personnal Mobile : "), cons);
		cons.gridx = 3;
		add(mobilePhone, cons);

		cons.gridy++;
		cons.gridx = 0;
		add(new JLabel("Personnal mail : "), cons);
		cons.gridx = 1;
		add(mailPerso, cons);
		mailPerso.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (Desktop.isDesktopSupported()) {
					try {
						Desktop.getDesktop().mail(
								new URI("mailto:" + mailPerso.getText()));
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						JOptionPane
								.showMessageDialog(null,
										"Error : no mail client specified or an error occured");
					} catch (URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						JOptionPane
								.showMessageDialog(null,
										"Error : no mail client specified or an error occured");
					}
				}
			}
		});

		cons.gridy++;
		cons.gridx = 0;
		add(new JLabel("Website : "), cons);
		cons.gridx = 1;
		add(homePage, cons);
		homePage.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (Desktop.isDesktopSupported()) {
					try {
						String url = homePage.getText();
						if(!url.contains("http://") && !url.contains("https://")
								&& !url.contains("www."))
							url="http://"+url;
						Desktop.getDesktop()
								.browse(new URI(url));
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
	}
}
